﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace API.Models.Repository
{
    public class WH_COMPANIES_Repository : GenericRepository<PartnerDbContext, WH_COMPANIES>
    {
        /*
        public IEnumerable<WH_COMPANIES> GetAllowedCompanies(WH_COMPANIES company)
        {
            var companies_A = this._entities.SEC_COMPANIES_ACCESS
                .Where(r => r.IS_ALLOWED && r.COMPANY_GUID == company.GUID_RECORD)
                .Join(this._entities.WH_COMPANIES, a => a.ALLOW_TO_GUID, b => b.GUID_RECORD, (a, b) => b)
                .ToList();

            var companies_B = this._entities.SEC_COMPANIES_ACCESS
                .Where(r => r.IS_ALLOWED && r.ALLOW_TO_GUID == company.GUID_RECORD)
                .Join(this._entities.WH_COMPANIES, a => a.COMPANY_GUID, b => b.GUID_RECORD, (a, b) => b)
                .ToList();

            var companies_C = companies_A
                .Union(companies_B)
                .ToList();

            companies_C.Add((WH_COMPANIES)company);

            return companies_C;
        }
        */

        public override IQueryable<WH_COMPANIES> Find(System.Linq.Expressions.Expression<Func<WH_COMPANIES, bool>> predicate)
        {
            IQueryable<WH_COMPANIES> query = _entities.WH_COMPANIES;

            if (predicate != null)
                query = query.Where(predicate);

            if (session != null)
            {
                query = query
                    .ToList()
                    .Where(r => r.OWNER_GUID == session.COMPANY_GUID)
                    //.Where(r => session.USER.COMPANIES.Select(n => n.COMPANY_GUID).Contains(r.GUID_RECORD))
                    .AsQueryable();
            }

            return query;
        }

        public override void Add(WH_COMPANIES entity)
        {
            var record = this.Find(r =>
                    r.OWNER_GUID == this.session.COMPANY_GUID &&
                    r.EXT_KEY == entity.EXT_KEY)
                .SingleOrDefault();

            if (record != null)
            {
                record.COMPANY_NAME = entity.COMPANY_NAME;
                record.EXT_KEY = entity.EXT_KEY;
                record.NOTE = entity.NOTE;

                record.DATECREATE = DateTime.Now;
                record.DATEUPDATE = DateTime.Now;

                if (this.session != null)
                {
                    record.USERCREATE_GUID = this.session.USER_GUID;
                    record.USERUPDATE_GUID = this.session.USER_GUID;
                }

                this.Edit(record);
                return;
            }

            if (entity.GUID_RECORD == null || entity.GUID_RECORD == Guid.Empty)
                entity.GUID_RECORD = Guid.NewGuid();

            entity.OWNER_GUID = this.session.COMPANY_GUID;

            entity.DATECREATE = DateTime.Now;
            entity.DATEUPDATE = DateTime.Now;

            if (this.session != null)
            {
                entity.USERCREATE_GUID = this.session.USER_GUID;
                entity.USERUPDATE_GUID = this.session.USER_GUID;
            }

            _entities.Set<WH_COMPANIES>().Add(entity);
        }

        public override void Edit(WH_COMPANIES entity)
        {
            entity.OWNER_GUID = this.session.COMPANY_GUID;

            entity.DATECREATE = DateTime.Now;
            entity.DATEUPDATE = DateTime.Now;

            if (this.session != null)
            {
                entity.USERCREATE_GUID = this.session.USER_GUID;
                entity.USERUPDATE_GUID = this.session.USER_GUID;
            }

            try
            {
                if (_entities.Entry(entity).State == System.Data.EntityState.Modified)
                {
                    _entities.Entry(entity).CurrentValues.SetValues(entity);
                }

                _entities.Entry(entity).State = System.Data.EntityState.Modified;
            }
            catch
            {
                /// В случае повторного обновления записи
                /// возникает ошибка
                /// нужно как-то обработать. Но как?
            }
        }
    }
}